<?php
namespace app\home\controller;
use think\Validate;	
use think\Session;	

class Login extends \think\Controller
{
	public function checklogin()
	{
	  $account    = input('post.account');
	  $user_pwd   = input('post.user_pwd');
	  if(empty($account))
	  {
	  	$this->error("账号不可以为空");
	  }
	  else
	  {
		  	if(empty($user_pwd))
		  {
		  	$this->error("密码不可以为空");
		  }
	  }
	  $ret1 = db('user')->where("user_tell='{$account}'")->find();
	  if(empty($ret1))
	  {
	  	$this->error("该账号没有被注册","/home/reg/reg");
	  }
	  $user_pwd = md5($user_pwd);
	  $ret = db('user')->where("user_tell='{$account}' and user_pwd='{$user_pwd}'")->find();

	  if($ret == false)
	  {
	  	$this->error("登陆失败");
	  }
	  else
	  {
	  	$_SESSION['user_id'] = $ret['user_id'];
	  	session('user_id', $ret['user_id']);
	  	if(session('url'))
	  	{
	  		$this->success("登录成功",session('url'));  //记得加上登陆成功后跳转的页面
	  	}
	  	else
	  	{
	  		$this->success("登录成功",'/home/index/index');  //记得加上登陆成功后跳转的页面
	  	}
	  	
	  }
	}

	public function login() 
	{
       return view('login');
    }

	public function logout() 
	{
        Session::clear();
        $this->redirect('home/login/login');
    }
}
